package com.css.handler;

import com.alibaba.fastjson.JSON;
import com.css.Main;
import com.css.entity.*;
import com.css.util.DateUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import okhttp3.Call;
import okhttp3.MediaType;

import java.util.*;

/**
 * <p>
 * -  上传控制器
 * </p>
 *
 * @author css
 * @since 2021/4/7
 */
public class UploadHandler {
    public void upload() {
        new Thread(() -> {

            while (true) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //判断是否有按键按下，如果没有键盘按下，提交次数
                if (KeyMapWrapper.isPress()) {
                    continue;
                }

                List<KeyBorder> keyBorders = new ArrayList<>();
                Set<Map.Entry<String, Integer>> keyList = NeedUploadMap.getKeyList();
                for (Map.Entry<String, Integer> stringIntegerEntry : keyList) {
                    String keyName = stringIntegerEntry.getKey();
                    Integer pressNum = stringIntegerEntry.getValue();
                    String createTime = DateUtil.FORMAT.format(new Date());
                    KeyBorder keyBorder = new KeyBorder();
                    keyBorder.setKeyName(keyName);
                    keyBorder.setCreateTime(createTime);
                    keyBorder.setPressNum(pressNum);
                    keyBorders.add(keyBorder);
                }
                if (!keyBorders.isEmpty()) {
                    OkHttpUtils.postString().url(Constant.BASE_HTTP + "/keyBorder/keyUp")
                            .addHeader("Content-Type", "application/json")
                            .addHeader("userId", Main.getUser().getId().toString())
                            .mediaType(MediaType.parse("application/json; charset=utf-8"))
                            .content(JSON.toJSONString(keyBorders))
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int i) {
                                    e.printStackTrace();
                                    rollBack(keyBorders);
                                }

                                @Override
                                public void onResponse(String s, int i) {
//                                    System.out.println(s);
                                    Result result = JSON.parseObject(s, Result.class);
                                    if (!result.isSuccess()) {
                                        rollBack(keyBorders);
                                    }
                                }
                            });
                    keyList.clear();
                }
            }
        }).start();
    }

    public void rollBack(List<KeyBorder> data) {
        NeedUploadMap.rollBack(data);
    }

    public void start() {
        upload();
    }
}
